package com.mixsmart.commonweb.upload.classify;

import java.util.List;

import com.mixsmart.commonweb.upload.ClassifyModel;
import com.mixsmart.commonweb.utils.SpringContextUtils;
import com.mixsmart.utils.CollectionUtils;

/**
 * 上传文件归类工厂
 * @author lmq
 * @version 1.0
 * @since JDK1.6以上
 * <br />
 * 2016年7月22日
 */
public class UploadClassifyFactory {
	
	private static List<AbstractUploadClassify> uploadClassifies;
	
	/**
	 * 获取文件上传后归类的位置
	 * @param uploadClassifyType 上传文件归类类型
	 * @param classifyModel 归类模板
	 * @return 返回归类目录
	 */
	public static String getClassifyDir(String uploadClassifyType, ClassifyModel classifyModel) {
		String classifyDirStr = null;
		AbstractUploadClassify uploadClassify = getInstance(uploadClassifyType);
		if(null != uploadClassify) {
			classifyDirStr = uploadClassify.classifyDir(classifyModel);
		}
		return classifyDirStr;
	}
	
	/**
	 * 根据类型获取AbstractUploadClassify的具体实现类
	 * @param uploadClssifyType
	 * @return
	 */
	private static AbstractUploadClassify getInstance(String uploadClssifyType) {
		AbstractUploadClassify uploadClassify = null;
		if(CollectionUtils.isEmpty(uploadClassifies)) {
			uploadClassifies = SpringContextUtils.finds(AbstractUploadClassify.class);
		}
		for (AbstractUploadClassify uploadClassifyTmp : uploadClassifies) {
			if(uploadClassifyTmp.getName().equals(uploadClssifyType)) {
				uploadClassify = uploadClassifyTmp;
				break;
			}
		}
		return uploadClassify;
	}
}
